import json

import requests


def get_token():
    url = 'https://api.weixin.qq.com/cgi-bin/token'
    param = {
        "grant_type": "client_credential",
        "appid": "wx147013caa3a12a8a",
        "secret": "1bab6048c4c6a366339cf43caa496534"
    }
    res = requests.get(url, param)
    res = json.loads(res.content)
    return res["access_token"]


if __name__ == "__main__":

    token = get_token()

    url = f'https://api.weixin.qq.com/cgi-bin/message/template/send?access_token={token}'
    data = {
        "touser": "opZ8566qncOq1hNOK4YhyfiNr4Kc",
        "template_id": "lmsX83fevfISQJbf76FRZ8ZnD0beOC7a_9wIlc4atYw",
        "data": {
            "first": {
                "value": "恭喜你购买成功！",
                "color": "#173177"
            },
            "remark": {
                "value": "欢迎再次购买！",
                "color": "#173177"
            }
        }
    }
    res = requests.post(url, json=data)
    print(res.text)
